package com.demo.testui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Toast;

//方法3 使用view的监听
//首先通过getViewTreeObserver()获取到ViewTreeObserver实例，然后调用addOnGlobalLayoutListener()添加监听。
//原理：当我们注册这个监听时，控件经过 onMeasure->onLayout->onDraw一系列方法渲染完成后会去回调这个注册的监听。
//缺点：这个方法也会被调用多次
public class ActivitySolve3 extends Activity {
    String TAG = Tags.solve3;
    View mView;
    MyView mMyView;
    MyViewGroup mMyViewGroup;
    float percent =Constant.PERCENT_SHOW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_view_size);
        //重置自定义的View
        MyView.percent = Constant.PERCENT_DEFAULT;
        MyViewGroup.percent = Constant.PERCENT_DEFAULT;

        init();
    }

    private void init() {
        mView = findViewById(R.id.click_area_view);
        mMyView = findViewById(R.id.click_area_myview);
        mMyViewGroup = findViewById(R.id.click_area_myviewgroup);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"View",Toast.LENGTH_SHORT).show();
            }
        });
        mMyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"MyView",Toast.LENGTH_SHORT).show();
            }
        });
        mMyViewGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"MyViewGroup",Toast.LENGTH_SHORT).show();
            }
        });




        /** 方法3 使用view的监听 **/

        mView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setClickSize(mView, percent, "  View     ");
            }
        });

        mMyView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mMyView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setClickSize(mMyView, percent, "MyView     ");
            }
        });

        mMyViewGroup.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setClickSize(mMyViewGroup, percent, "MyViewGroup");
            }
        });
    }

    private void setClickSize(View view, float percent, String tag) {
        Log.e(TAG, tag + "size:[" + view.getMeasuredWidth() + "," + view.getMeasuredHeight() + "]");
        ViewGroup.LayoutParams btnLayoutParams = (ViewGroup.LayoutParams) view.getLayoutParams();
        btnLayoutParams.width = (int) (view.getMeasuredWidth() * percent);
        btnLayoutParams.height = (int) (view.getMeasuredHeight() * percent);
        view.setLayoutParams(btnLayoutParams);
    }




}
